<template>
	<view class="content" :class="theme+'-bg-middle'">

		<scroll-view scroll-y="true" style="height: 100%;">
			<view class="operationBox" v-if="isCheckdShow" :class="theme+'-bg-abox'">
				<view class="operation-l">
					<view class="icon" @click="isCheckdShow=false">
						<image src="../../static/images/icons/cross-fill.png"></image>
					</view>
					<view class="title">
						<text>{{$t('home.Favoritesedit')}}</text>
					</view>
				</view>
				<view class="operation-r">
					<view class="icon" @click="selectAll">
						<image src="../../static/images/icons/quanxuan.png"></image>
					</view>
					<view class="icon" @click="back">
						<image src="../../static/images/icons/fanxuan.png"></image>
					</view>
					<view class="icon">
						<image src="../../static/images/icons/paixu.png"></image>
					</view>
				</view>

			</view>

			<view class="box" v-if="collectList.length" :style="isCheckdShow?'padding-top:44px ;':''">
				<view class="Tools" @longpress="isCheckdShow = true" v-for="(item,index) in collectList"
					@click="isCheckdShow ? check(index) : go(item.url)" :key="item.id">
					<tools :item="item" :isCheckdShow="isCheckdShow"></tools>
				</view>
			</view>

			<view class="none" v-else>
				<view class="imgBox">
					<image :src="`../../static/images/img/${theme}-meiyoushoucang.png`"></image>
				</view>
				<view class="btn" @click="got()" :class="theme+'-bg-abox'">
					<text>{{$t('home.Nocollection')}}</text>
					<view class="icon">
						<uni-icons type="plusempty" size="50"></uni-icons>
					</view>
				</view>
			</view>

			<view class="bttn br-4" :class="theme+'-bg-abox'" @click="remove" v-if="isCheckdShow">
				<uni-icons type="trash" color="#ffffff" size="50"></uni-icons>
			</view>
		</scroll-view>

	</view>
</template>

<script>
	import tools from './components/Tools.vue'
	export default {
		components: {
			tools,
		},
		data() {
			return {
				collectList: [],
				arr: [],
				isCheckdShow: false,
				is: false
			}
		},
		onLoad() {
			this.$theme()
		},
		onShow() {
			this.$theme()
			this.getData()
		},
		onHide() {
			this.isCheckdShow = false
		},
		methods: {
			go(link) {
				uni.navigateTo({
					url: link
				})
			},
			got() {
				uni.navigateTo({
					url: './add-collection/AddCollection'
				})
			},
			check(index) {
				this.collectList[index].isCheckd = !this.collectList[index].isCheckd
			},
			selectAll() {
				this.collectList.forEach(item => {
					item.isCheckd = true
				})
			},
			back() {
				this.collectList.forEach(item => {
					item.isCheckd = !item.isCheckd
				})
			},
			getData() {

				uni.getStorage({
					key: 'collectList',
					success: (res) => {
						this.collectList = res.data
					}
				})

			},
			weChatLogin() {
				//#ifdef MP-WEIXIN
				uni.checkSession({
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						console.log(err)
					}
				})
				uni.getUserProfile({
					desc: '登录',
					success: (res1) => {
						// console.log(res1.userInfo)
						uni.showLoading({
							title: '登录中...'
						})
						uni.getProvider({
							service: 'oauth',
							success: (res2) => {
								// console.log(res2)
								if (~res2.provider.indexOf('weixin')) {
									uni.login({
										provider: 'weixin',
										success: (res3) => {
											// console.log(res3)

											this.$post('/login/wx-mini', {
												code: res3.code
											}).then(res4 => {

												// console.log(res4.data)
												uni.hideLoading()
												uni.showToast({
													title: "登录成功",
													icon: 'none'
												});
												res4.data.nickname = res1
													.userInfo.nickName
												this.userInformation = res4
													.data
												uni.setStorage({
													key: 'userInformation',
													data: res4.data
												})
											})
										},
										fail: () => {
											uni.showToast({
												title: "微信登录授权失败",
												icon: "none"
											});
										}
									})

								} else {
									uni.showToast({
										title: '请先安装微信或升级版本',
										icon: "none"
									});
								}
							}
						});
					},
					fail: () => {
						// console.log(777)
						// uni.showToast({
						// 	title: "微信登录授权失败",
						// 	icon: "none"
						// });
					}
				})
				//#endif
			},
			remove() {
				if (!uni.getStorageSync('userInformation').token) {
					// #ifdef APP-PLUS
					return uni.showToast({
						title: '需要登录才能收藏哦。',
						icon: 'none',
						success: () => {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/mine/my-account/login/Login'
								})
							}, 500)
						}
					})
					// #endif
					// #ifdef MP-WEIXIN
					uni.showToast({
						title: '还未登录呢，请先登录',
						icon: 'none',
						success: () => {},
					})
					this.weChatLogin()
					return
					// #endif
				}

				let arr = []

				this.collectList.forEach((item, index) => {
					if (item.isCheckd == true) {
						arr.push(item.id)
					}
				})

				let str = arr.join(',')

				if (arr.length) {
					this.$post('/tool/collect', {
						toolId: str, //1,2 工具的id，多个工具以英文逗号分割
						type: 1, //1：收藏；2点赞
						action: 0 //0：取消收藏/点赞；1：收藏/点赞
					})

					this.collectList = this.collectList.filter(item => !item.isCheckd)

					uni.setStorage({
						key: 'collectList',
						data: this.collectList,
					})

					this.isCheckdShow = false

					uni.showToast({
						title: '编辑完成。',
						icon: 'none'
					})
				} else {
					this.isCheckdShow = false
					uni.showToast({
						title: '编辑完成。',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 100%;

		.box {
			display: flex;
			flex-wrap: wrap;
			padding-bottom: 30rpx;
		}

		.Tools {
			margin: 30rpx 0 0 30rpx;
		}

		.none {
			position: absolute;
			top: 25%;
			left: 25%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.imgBox {
				width: 400rpx;
				height: 400rpx;
			}

			.btn {
				width: 300rpx;
				height: 105rpx;
				display: flex;
				align-items: center;
				border-radius: 50rpx;
				opacity: 0.5;
				color: #FFFFFF;
				position: relative;
				padding-left: 70rpx;


				text {
					font-size: 32rpx;
				}

				.icon {
					position: absolute;
					right: 10rpx;
					width: 100rpx;
					height: 100rpx;
					background-color: #FFFFFF;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;

					.uni-icons {
						font-size: 70rpx;
					}
				}
			}


		}

		.operationBox {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			width: 100%;
			height: 88rpx;
			padding: 0 40rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.operation-l {
				display: flex;
				flex-direction: row;

				.icon {
					width: 40rpx;
					height: 40rpx;
				}

				.title {
					font-size: 30rpx;
					margin-left: 40rpx;
					color: #FFFFFF;
				}
			}

			.operation-r {
				display: flex;
				flex-direction: row;

				.icon {
					width: 40rpx;
					height: 40rpx;
					margin-left: 30rpx;
				}

			}
		}

		.bttn {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
			position: fixed;
			right: 40rpx;
			bottom: 140rpx;
			z-index: 999;

			.uni-icons {
				font-size: 60rpx;
			}
		}
	}
</style>
